package com.boot2.core.web.websocket;

import com.boot2.core.model.vo.Result;
import com.boot2.core.utils.ResultBuilder;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.converter.MessageConverter;
import org.springframework.messaging.support.GenericMessage;

/**
 * @author zhangweilin
 * @Description: 将发出去的对象自动用Result包装一下
 * @date 2020/7/23
 */
public class ResultMessageConverter implements MessageConverter {
    @Override
    public Object fromMessage(Message<?> message, Class<?> targetClass) {
//        System.out.println("message = " + message);
//        System.out.println("targetClass = " + targetClass);
        //todo 收消息再做
        return null;
    }

    @Override
    public Message<?> toMessage(Object payload, MessageHeaders headers) {
        Result<Object> result = ResultBuilder.buildResult(payload);
        GenericMessage<Result<Object>> genericMessage = new GenericMessage<>(result,headers);
        return genericMessage;
    }
}
